// src/db_test.cpp
#include <iostream>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include "../data.hpp"

using namespace aod;

MYSQL* InitMysql() {
    MYSQL *mysql = mysql_init(NULL);
    if(mysql == NULL) {
        std::cerr << "mysql_init failed!" << std::endl;
        return nullptr;
    }

    if(mysql_real_connect(mysql, "127.0.0.1", "qq", "123456", "aod_system", 0, NULL, 0) == NULL) {
        std::cerr << "mysql_real_connect failed: " << mysql_error(mysql) << std::endl;
        mysql_close(mysql);
        return nullptr;
    }

    mysql_set_character_set(mysql, "utf8");
    std::cout << "Database connected!" << std::endl;
    return mysql;
}

void TestInsert(TableVideo& tb_video) {
    std::cout << "\n--- Testing Insert ---" << std::endl;
    Json::Value new_video;
    new_video["name"] = "Linux C++";
    new_video["info"] = "A course about building servers.";
    new_video["video"] = "/video/linux_cpp.mp4";
    new_video["image"] = "/img/linux_cpp.png";

    if(tb_video.Insert(new_video)) {
        std::cout << "Insert success!" << std::endl;
    } else {
        std::cout << "Insert failed!" << std::endl;
    }
}

void TestSelectAll(TableVideo& tb_video) {
    std::cout << "\n---- Testing SelectAll ---" << std::endl;
    Json::Value all_videos;

    if(tb_video.SelectAll(&all_videos)) {
        std::cout << "SelectAll success!" << std::endl;

        // // 优雅打印 JSON
        // Json::StyledWriter writer;
        // std::cout << writer.write(all_videos) << std::endl;
        Json::StreamWriterBuilder wbuilder;
        wbuilder["indentation"] = "  ";
        wbuilder["emitUTF8"] = true;

        std::unique_ptr<Json::StreamWriter> writer(wbuilder.newStreamWriter());
        

        writer->write(all_videos, &std::cout);
        std::cout << std::endl;
    } else {
        std::cout << "SelectAll failed!" << std::endl;
    }
}

int main() {
    // 1. 初始化一个 MYSQL 句柄
    MYSQL* mysql_handle = InitMysql();
    if(mysql_handle == nullptr) {
        return 1;
    } 

    // 2. 讲 C API 风格的句柄 注入 到 C++类
    TableVideo video_db(mysql_handle);
    // TableVideo *video = new TableVideo(mysql_handle);

    // 3. 执行测试
    std::cout << "---- Cleaning up old data ----" << std::endl;
    video_db.DeleteAll();

    TestInsert(video_db);
    TestSelectAll(video_db);
    
    // TestInsert(video);
    // TestSelectAll(video);

    // 4. 测试完，统一关闭
    mysql_close(mysql_handle);
    std::cout << "\nDatabase connection closed." << std::endl;



    // -----------------------------------------------------------------
    // 
    //  创建 Mysql 测试
    //     
    // // 2. 初始化句柄 (遥控器)
    // MYSQL *mysql = mysql_init(NULL);
    // if (mysql == NULL) {
    //     std::cerr << "mysql_init failed!" << std::endl;
    //     return 1;
    // }

    // // 3. 连接数据库
    // // 细节：我们使用 root, 无密码, 访问 "aod_system" 库
    // if (mysql_real_connect(mysql, "127.0.0.1", "qq", "123456", "aod_system", 0, NULL, 0) == NULL) {
    //     // 如果连接失败，打印错误信息
    //     std::cerr << "mysql_real_connect failed: " << mysql_error(mysql) << std::endl;
    //     mysql_close(mysql); // 即使失败，也要关闭句柄
    //     return 1;
    // }

    // // 4. (如果连接成功) 打印服务器版本
    // std::cout << "Database connected!" << std::endl;
    // std::cout << "Server version: " << mysql_get_server_info(mysql) << std::endl;

    // // 5. 关闭连接，释放句柄
    // mysql_close(mysql);

    return 0;
}